package com.zhb.lkd.controller;

import com.zhb.lkd.base.ResponseResult;
import com.zhb.lkd.exception.BusinessException;
import com.zhb.lkd.utils.AliYunOssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/api/vm-service/sku")
@Slf4j
@Api(tags = "公共接口")
public class CommonController {
    @Resource
    private AliYunOssUtils aliYunOssUtils;

    @ApiOperation("图片上传")
    @PostMapping("/fileUpload")
    public ResponseResult fileUpload(MultipartFile fileName) throws IOException {
        // 校验是否为图片文件
        if (fileName.getSize() == 0) {
            throw new BusinessException(500, "上传图片不能为空");
        }
        // 获得原始文件名
        String originalFilename = fileName.getOriginalFilename();
        // 获得文件扩展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName1 = UUID.randomUUID().toString() + extension;
        String fileUrl = aliYunOssUtils.uploadFile(fileName1, fileName.getInputStream());
        return ResponseResult.ok(fileUrl);
    }
}
